def MCRun(fp):
    import random
    import GlobalParam as GP
    import math
    gp=GP.iparam
    # Assign a seed number
    if gp.seed<0:
        random.seed()
    elif gp.seed>=0:
        random.seed(gp.seed)
    else:
        raise Exception
    if gp.nexe==0 or gp.nrun==0:
        raise Exception("EXE or RUN is not specified.")
    nexe=0
    while True:
        nexe+=1
        if nexe>gp.nexe:
            break
        # Run [gp.nrun] times
        rtime1=0
        nrun=0
        gp.zerocount()
        while True:
            nrun+=1
            if nrun>gp.nrun:
                break
            rtime2=0
            flow_no=0
            stage_no=0
            while True:
                BreakFlag=False
                n_path=gp.afs1(flow_no,stage_no).get_npath()
                TotalProb=0.0
                for i in range(n_path):
                    TotalProb=TotalProb+gp.get_prob(flow_no,stage_no,i)
                if TotalProb == 0.0:
                    raise Exception("Total probability is zero.. (mcrun.py)")
                rn=random.random()*TotalProb
                ErrorFlag=0
                AccumProb=0.0
                path_no=-1
                for i in range(n_path):
                    AccumProb+=gp.get_prob(flow_no,stage_no,i)
                    if rn<AccumProb:
                        path_no=i
                        break
                if path_no<0:
                    raise Exception("Paths are not specified.. (mcrun.py)")
                # Estimate time
                rtime2=rtime2-math.log(random.random())/AccumProb
                # Update flow count
                gp.afs1(flow_no,stage_no).countflow(path_no)
                whatstage=gp.get_prop(flow_no,stage_no,path_no)
                # whatstage=-1 : This stage is the end itself.
                # whatstage=0 : Move to next stage
                # whatstage=1 : Move to other stage
                # whatstage=2 : Move to independent end
                if whatstage == -1:
                    BreakFlag=True
                elif whatstage == 0:
                    stage_no=gp.afs1(flow_no,stage_no).get_next_stage(path_no)
                elif whatstage == 1:
                    flow_no=gp.afs1(flow_no,stage_no).get_next_flow(path_no)
                    stage_no=gp.afs1(flow_no,stage_no).get_next_stage(path_no)
                elif whatstage == 2:
                    BreakFlag=True
                else:
                    raise Exception
                if BreakFlag:
                    gp.afs1(flow_no,stage_no).countstop(path_no)
                    break
            # Update total time
            rtime1=rtime1+rtime2

        # Calculate flow percentage
        # Calculate average time
        str1=" Exe = %d," % nexe
        str1=str1+" AvgTime=%4.2e(sec), " % (rtime1/float(gp.nrun))
        for i in range(gp.get_nout()):
            fn,sn,pn=gp.get_out(i)
            str1=str1+" %4.2e (%d) " % (float(gp.afs1(fn,sn).get_stopcount(pn))/float(gp.nrun),gp.afs1(fn,sn).get_stopcount(pn))
        fp.write(str1+'\n')
